#include "ExcelManager.h"
#include <QMutex>
#include "DebugTool.h"
//#include <windows.h>
//#include <windowsx.h>

ExcelManager* ExcelManager::self = 0;

/**
  * @brief : 原型 static ExcelManager *Instance();，懒汉模式
  * @retval: 返回当前的指针
  */
ExcelManager* ExcelManager::Instance() {
  QMutex mutex;
  if (!self) {
    QMutexLocker locker(&mutex);
    if (!self) {
      self = new ExcelManager;
    }
  }
  return self;
}

ExcelManager::~ExcelManager()
{
}
/**
  * @brief    : 设置Excel程序是否可见, 对于已经显示的Excel 无影响
  * @param : bool visible
  * @retval  :  void
  */
void ExcelManager::setVisible(bool visible = true) {
  Application->setProperty("Visible", visible);
}

/**
  * @brief    :   Excel Application退出，并自动删除Application
  *                   Application->dynamicCall("Quit()");
  * @param :    无
  * @retval:      None
  */
void ExcelManager::quit() {
  QObject::connect(Application, &QObject::destroyed,
                   [] { Dout << "Excel Application is destroyed"; });
  Application->dynamicCall("Quit()");
  Application->deleteLater();
}

/**
  * @brief :      增加新的工作簿
  *                   WorkBooks->dynamicCall("Add");
  * @retval: None
  */
void ExcelManager::addWorkBook() {
  WorkBooks->dynamicCall("Add");
   ActiveWorkbook = Application->querySubObject("ActiveWorkBook");
}

/**
  * @brief :      打开指定的工作簿，并获取
  * @param :   QString& fileName   文件名
  * @retval: None
  */
void ExcelManager::openWorkBook(QString& fileName) {
  WorkBooks->dynamicCall("Open(const QString&)", fileName);
  ActiveWorkbook = Application->querySubObject("ActiveWorkBook");
  qExcel->setVisible (true);
}

// 获取活动工作簿的 所有工作表的名称
/**
  * @brief :      获取活动工作簿的 所有工作表的名称
  * @param :
  * @param :
  * @retval: None
  */
void ExcelManager::getWorkSheetsName() {
  ActiveWorkbook = Application->querySubObject("ActiveWorkBook");
  WorkSheets = ActiveWorkbook->querySubObject("Sheets");
  int Count = WorkSheets->property("Count").toUInt();

  QList<QAxObject*> list;

  for (int i = 1; i <= Count; ++i) {
    auto sheet = WorkSheets->querySubObject("Item(int)", i);
    list << sheet;
  }

  sheetsNames.clear ();

  for (int i = 0; i < Count; ++i) {
    sheetsNames << list.at(i)->property("Name").toString();
  }

  Dout << sheetsNames;
}
// 获取当前活动的工作表
void ExcelManager::getActiveSheet()
{
    activeSheet = Application->querySubObject("ActiveSheet");
}

// 添加数据
void ExcelManager::appendToActSheet(QVariantList list)
{
    if(ActiveWorkbook==0) {
        addWorkBook ();
    }
    getActiveSheet ();
    QAxObject *usedRange = activeSheet->querySubObject("UsedRange");
    QAxObject *rows = usedRange->querySubObject("Rows");

    int  cr = rows->property ("Count").toInt ();
    int sr = usedRange->property ("Row").toInt ();
    Dout <<cr << sr;

    QAxObject *Range = activeSheet->querySubObject ("Cells(int,int)",cr+sr,1);
    Range = Range->querySubObject("Resize(int,int)", list.count() , 4);

    Range->setProperty ("Value", list );       // 可以设置属性的方法写入
    Range->deleteLater ();
    rows->deleteLater ();
    usedRange->deleteLater ();
}

// 获取指定行
QAxObject* ExcelManager::Rows(int row) {
  QAxObject* Row = Application->querySubObject("Rows(int)", row);
  Row->setProperty("Value", row);
  return Row;
}

// 构造函数
ExcelManager::ExcelManager(QObject *parent) {

  Application = new QAxObject("Excel.Application", parent);
  WorkBooks = Application->querySubObject(
      "WorkBooks");  // Application 退出后，WorkBooks会自动销毁
                     //    cout << WorkBooks->parent () << Application;

  // 程序退出，调用excle退出，否则会挂在后台
  QObject::connect(qApp, &QApplication::aboutToQuit, [this] { this->quit(); });
  setVisible(true);  // 默认显示
}
